#include "RocketManager.h"
#include "controller/MoveController/Box2dMoveController/Box2dMoveController.h"
#include "global/GlobalPath.h"
#include "global/GlobalDefine.h"

RocketManager::RocketManager(){
	m_rocket = NULL;
	prePos = ccp(0,0);
	curPos = ccp(0,0);
}

RocketManager::~RocketManager(){

}

bool RocketManager::init(){
	bool bRet = false;
	do{
		CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

		m_rocket = Rocket::create();

		CCSprite* rocketSprite = CCSprite::create(PATH_PNG_SPRITE_rocket_main);
		m_rocket->bindSprite(rocketSprite);
		m_rocket->bindSprite(CCArmature::create("rocket"));
		m_rocket->getArmature()->getAnimation()->playByIndex(0);

		m_rocket->setPosition(ccp(visibleSize.width/2 , rocketSprite->getContentSize().height));
		this->addChild(m_rocket);

		m_moveController = Box2dMoveController::create(m_rocket);
		m_rocket->setMoveController(m_moveController);

		this->setTouchEnabled(true);
		bRet = true;
	}while(0);

	return bRet;
}

bool RocketManager::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
	prePos = pTouch->getLocation();
	return true;
}

void RocketManager::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){
	curPos = pTouch->getLocation();

	float moveLen = curPos.x - prePos.x;
	m_rocket->moveByTouch(moveLen);

	prePos = curPos;
}

void RocketManager::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){

}

void RocketManager::registerWithTouchDispatcher(){
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
}

Rocket* RocketManager::getRocket(){
	return m_rocket;
}

Box2dMoveController* RocketManager::getBox2dMoveController(){
	return m_moveController;
}